package Server.lastproject;





import Server.MyDataType.MyStructTable;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;
import javax.print.DocFlavor.STRING;

/**
 * Component to be used as tabComponent;
 * Contains a JLabel to show the text and 
 * a JButton to close the tab it belongs to 
 */ 
public class ButtonTabComponent extends JPanel {
    private final JTabbedPane pane;
    private JTable TABLE;
    private ServerForm serverForm;
    private String _nameTableOwner;
    private int LenhKhiDong;
    public ButtonTabComponent(final JTabbedPane pane,JTable jTable,ServerForm form,int  LenhThucThi,String namTable) {
        //unset default FlowLayout' gaps
       
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
         TABLE = jTable;
         _nameTableOwner = namTable;
        serverForm = form;
        LenhKhiDong = LenhThucThi;
        this.pane = pane;
        setOpaque(false);
        
        //make JLabel read titles from JTabbedPane
        JLabel label = new JLabel() {
            @Override
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
        
        add(label);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //tab button
        JButton button = new TabButton();
        add(button);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    private class TabButton extends JButton implements ActionListener {
        
        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("close this tab");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            switch(LenhKhiDong)
            {
                case 1:
                    String sNewNameTable = (String) JOptionPane.showInputDialog(serverForm, "Name Table","Server",
                            JOptionPane.INFORMATION_MESSAGE,null,null,"NewTable");

                    //người dùng click cancel
                    if (sNewNameTable == null) {
                         CloseTab();
                        return;
                    }
                    if (sNewNameTable.equals("")) {
                        JOptionPane.showMessageDialog(serverForm, "Name table not null!", "Server", JOptionPane.ERROR_MESSAGE, null);
                        return;
                    }
                    MyStructTable structTable = serverForm.getMyStructTable(TABLE.getModel());
                    boolean bCreateNewTable = false;
                    bCreateNewTable = serverForm.createTable(structTable, sNewNameTable);
                    serverForm.LoadDataTable();
                    if (!bCreateNewTable) {
                        JOptionPane.showMessageDialog(serverForm, "Save Structure table "+_nameTableOwner + " fail!", "Server", JOptionPane.ERROR_MESSAGE, null);
                    } 
                    else
                        CloseTab();
                    break;
                case 2:
                    int result = JOptionPane.showConfirmDialog(serverForm, "Do you want to saves change structure table "+_nameTableOwner, "Server", JOptionPane.YES_NO_CANCEL_OPTION );
                    if(result == JOptionPane.CANCEL_OPTION)
                        return;
                    if(result == JOptionPane.NO_OPTION)
                    {
                            CloseTab();
                            return;
                    }
                    
                        MyStructTable myTable = serverForm.getMyStructTable(TABLE.getModel());                        
                        boolean bUpdate = false;
                        int iSize = myTable._vtAttribute.size();
                        if(iSize != 0)
                        {    
                            bUpdate = serverForm.updateStructTable(myTable, _nameTableOwner);                        
                        }
                        
                        if(!bUpdate)
                        {
                            JOptionPane.showMessageDialog(serverForm, "Update structure table "+_nameTableOwner + " fail !", "Server", JOptionPane.ERROR_MESSAGE, null );
                        }
                        else
                        
                            CloseTab();
                    break;
                default: 
                        CloseTab();
                    break;
            }
            
           
            
            
        }

        private void CloseTab() {
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
               if (i != -1) {
                   pane.remove(i);
               }
        }
        
        //we don't want to update UI for this button
        @Override
        public void updateUI() {
        }

        //paint the cross
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //shift the image for pressed buttons
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.YELLOW);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}


